﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Temp.Controller;
using Temp.Model;
using Temp.NLG;
using Temp.Planner2;
using Windows.UI.Xaml;

namespace Temp.Minigame.Minigame3
{
    class Game3
    {
        ContentManager Content;
        private List<Conversation> convo;
        private List<Conversation> convo2;
        private List<Conversation> convoDesc;

        private List<Ontology> allItems;
        private List<Item> allAssets;
        private List<Texture2D> allTextures;
        private List<bool> isFinished;

        AlexGame alexGame;
        private ItemController itemctr;
        private Describer idesc;
        private List<String> descs;
        private string curString, curFeedback;
        private Rectangle mouseBox, continueBox;
        private MouseState mouse, mousePast;
        private Rectangle itemArea, bioArea, nonbioArea;
        private Texture2D[] items;
        private string[] itemNames;
        private Texture2D textBox, bioPic, bioClickedPic, nonbioPic, nonbioClickedPic, continueButton;
        private bool bioClicked, nonbioClicked;
        private Random ran;
        private int cur, finished;
        private Texture2D peer;
        private bool directions;
        private StoryPlanner storyplanner;

        public Game3(ContentManager content, AlexGame alexGame, StoryPlanner storyplanner)
        {
            this.Content = content;
            this.alexGame = alexGame;
            this.storyplanner = storyplanner;
        }

        public void Initialize()
        {
            bioArea = new Rectangle(10, 10, 480, 600);
            nonbioArea = new Rectangle(876, 10, 480, 600);
            itemArea = new Rectangle(558, 100, 250, 250);
            mouseBox = new Rectangle(0, 0, 30, 30);
            continueBox = new Rectangle(975, 700, 153, 56);
            itemctr = new ItemController();

            allItems = new List<Ontology>();
            allAssets = new List<Item>();
            allTextures = new List<Texture2D>();
            idesc = new Describer(storyplanner);
            finished = 0;
            curString = "";
            curFeedback = "";
            bioClicked = false;
            nonbioClicked = false;
            ran = new Random();
            isFinished = new List<bool>();
            directions = false;
        }

        private void getRan()
        {
            do
            {
                cur = ran.Next(allItems.Count);
            } while (isFinished[cur] != false);

            if (allItems[cur].concept2.StartsWith("non"))
            {
                convoDesc = idesc.getItemDescription2(allItems[cur].concept1, "<object> is <madeOf>.\nWhere does it belong?");
                curString = convoDesc.ElementAt(0).dialogue;
            }
            else
            {
                convoDesc = idesc.getItemDescription2(allItems[cur].concept1, "<object> <isA>.\nWhere does it belong?");
                curString = convoDesc.ElementAt(0).dialogue;
            }
        }
        public void loadContent()
        {
            getItems();
            loadItems();


            textBox = Content.Load<Texture2D>("Game3/storyBox");
            bioPic = Content.Load<Texture2D>("Game3/bio");
            bioClickedPic = Content.Load<Texture2D>("Game3/bio_click");
            nonbioPic = Content.Load<Texture2D>("Game3/nonbio");
            nonbioClickedPic = Content.Load<Texture2D>("Game3/nonbio_click");

            peer = Content.Load<Texture2D>("Peer/Boy/peer.png");
            continueButton = Content.Load<Texture2D>("UI elements/continue");
            getRan();
        }

        private void getItems()
        {
            allItems = itemctr.getNeededItems("is", "biodegradable");
            allItems.AddRange(itemctr.getNeededItems("is", "non-biodegradable"));

            foreach (Ontology it in allItems)
            {
                allAssets.Add(itemctr.getItem(it.concept1+"3"));
                isFinished.Add(false);
            }
        }

        private void loadItems()
        {
            foreach (Item it in allAssets)
            {
                allTextures.Add(Content.Load<Texture2D>(it.pic));
            }
        }

        public void Update()
        {
            if (finished < allItems.Count-1)
            {
                System.Diagnostics.Debug.WriteLine("GAME NOT FINISHED !!!!" + "allitems count" + allItems.Count + "finished val:" + finished);
                mousePast = mouse;
                mouse = Mouse.GetState();
                mouseBox.X = mouse.X;
                mouseBox.Y = mouse.Y;
                checkBoxClick();
                //System.Diagnostics.Debug.WriteLine("GAME NOT FINISHED !!!!" + "allitems count" + allItems.Count + "finished val:" + finished);
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("GAME FINISHED !!!!");
                alexGame.inMinigame3 = false;
                alexGame.drawAlexWorld = true;
            }
        }

        private void checkBoxClick()
        {
            if (mouse.LeftButton == ButtonState.Pressed && directions == true)
            {
                if (mouseBox.Intersects(bioArea))
                    bioClicked = true;
                else if (mouseBox.Intersects(nonbioArea))
                    nonbioClicked = true;
            }
            else if (mouse.LeftButton == ButtonState.Released && mousePast.LeftButton == ButtonState.Pressed)
            {
                if (directions == false && mouseBox.Intersects(continueBox))
                {
                    directions = true;
                }
                else if (mouseBox.Intersects(bioArea))
                {
                    bioClicked = false;
                    checkIfCorrect(true);
                }
                else if (mouseBox.Intersects(nonbioArea))
                {
                    nonbioClicked = false;
                    checkIfCorrect(false);
                }
            }
        }

        private void checkIfCorrect(bool isBio)
        {
            bool actualIsBio;
            if (allItems[cur].concept2.StartsWith("non"))
            {
                actualIsBio = false;
            }
            else
                actualIsBio = true;

            if (isBio == actualIsBio)
                correct();
            else wrong();
        }

        private void correct()
        {
            isFinished[cur] = true;
            finished++;
            curFeedback = "Nice! How about this one?\n";
            getRan();
        }

        private void wrong()
        {
            curFeedback = "Are you sure that's where it goes?\n";
        }

        public void Draw(SpriteBatch _spriteBatch)
        {

            if (bioClicked)
                _spriteBatch.Draw(bioClickedPic, bioArea, Color.White);
            else
                _spriteBatch.Draw(bioPic, bioArea, Color.White);

            if (nonbioClicked)
                _spriteBatch.Draw(nonbioClickedPic, nonbioArea, Color.White);
            else
                _spriteBatch.Draw(nonbioPic, nonbioArea, Color.White);

            _spriteBatch.Draw(allTextures[cur], new Rectangle(558,100,allAssets[cur].dimX,allAssets[cur].dimY), Color.White);

            _spriteBatch.Draw(textBox, new Vector2(255, 520), Color.White);
            _spriteBatch.Draw(peer, new Vector2(15, 500), Color.White);

            if (directions == false)
            {
                _spriteBatch.DrawString(Content.Load<SpriteFont>("Game3/Fonty"), "Let's help Gabby sort out his garbage!\nSorting waste is very important for the environment!\n\nOld and finished food belong in the biodegradable trash can.\nThings that can be used again belong in the nonbiodegradable trash can!\nClick on the trash can you think the item in the middle belongs!", new Vector2(325, 555), Color.Black);
                _spriteBatch.Draw(continueButton, new Vector2(975, 700), Color.White);
            }
            else
            {
                _spriteBatch.DrawString(Content.Load<SpriteFont>("Game3/Fonty"), curFeedback + curString, new Vector2(325, 555), Color.Black);

            }


        }
    }
}
